# -*-coding:utf-8-*-
import os
import time
import datetime
from PIL import Image
from pptx import Presentation
from pptx.util import Inches
from pptx.util import Pt
from pptx.enum.text import MSO_AUTO_SIZE
from pptx.enum.text import PP_ALIGN
from pptx.enum.shapes import MSO_SHAPE

# 项目及项目包括的物业内容
dict = {
    '公园商场部分': ['公园'],
    '桂花市场部分': ['桂花'],
    '苏白路市场部分': ['苏白路'],
    '中兴市场部分': ['中兴'],
    '房产大厦部分': ['大厦'],
    '河畔小区部分': ['河畔'],
    '海棠小区部分': ['海棠'],
    '沙柳小区部分': ['沙柳'],
    '电业小区部分': ['电业'],
    '保障物业部分': ['香柏', '府西', '广场', '湖东', '银行'],
    '唯美尚品部分': ['唯美']
}
# 涉及到的项目及物业
sjwy = {}
# 使用模板1.pptx
prs = Presentation('1.pptx')
# 总页数
ye = 0
# 临时变量 项目开始页
ksye = 1
# 对ppt的修改
# left, top, width, height = Inches(1), Inches(4.5), Inches(2), Inches(2)  # 预设位置及大小
htleft, httop, htwidth, htheight = Inches(0.16), Inches(0.16), Inches(13), Inches(6.5)
lleft, ltop, lwidth, lheight = Inches(0.74), Inches(0.27), Inches(6.42), Inches(6.97)
s1left, s1top, s1width, s1height = Inches(0.49), Inches(0.276), Inches(5.91), Inches(6.075)
s2left, s2top, s2width, s2height = Inches(6.88), Inches(0.276), Inches(5.91), Inches(6.075)
dkleft, dktop, dkwidth, dkheight = Inches(3.665), Inches(6.8), Inches(6.724), Inches(0.543)
ykleft, yktop, ykwidth, ykheight = Inches(8.484), Inches(3.283), Inches(4.106), Inches(2.488)
# 取电感器时间格式
nowtime = time.strftime('%Y%m%d', time.localtime(time.time()))
nowtime1 = time.strftime('%Y-%m-%d', time.localtime(time.time()))


# # 取本周开始到结束字符串

def get_current_week():
    monday, friday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    while friday.weekday() != 4:
        friday += one_day
    lsc = str(monday.month) + '月' + str(monday.day) + '日-' + str(friday.month) + '月' + str(friday.day) + '日'

    return lsc


# # 添加包括参数字符页
def add_ye(str):
    global ye, list, prs
    for i in range(0, len(list)):
        filename = list[i][:list[i].find('.')]
        if (filename.find('(') != -1):
            filename = filename[:filename.find('(')]
        filePath = os.path.join(rootdir, list[i])
        if os.path.splitext(filePath)[1] != '.jpg' or filename.find(str) < 0:
            continue
        im = Image.open(filePath)
        ye = ye + 1
        if (im.size[0] > im.size[1]):
            slide = prs.slides.add_slide(prs.slide_layouts[6])
            tx1 = slide.shapes.add_textbox(left=dkleft, top=dktop, width=dkwidth, height=dkheight).text_frame
            tx1.paragraphs[0].text = filename
            tx1.paragraphs[0].font.NameFarEast = '微软雅黑'  # name 为西文字体
            tx1.paragraphs[0].font.nameother = '微软雅黑'
            tx1.paragraphs[0].font.name = '微软雅黑'
            tx1.paragraphs[0].font.nameascii = '微软雅黑'
            tx1.paragraphs[0].font.size = Pt(30)
            tx1.paragraphs[0].alignment = PP_ALIGN.CENTER  # 文本内容对齐方式 居中
            slide.shapes.add_picture(filePath, htleft, httop, htwidth, htheight)
        else:
            slide = prs.slides.add_slide(prs.slide_layouts[6])
            tx1 = slide.shapes.add_textbox(ykleft, yktop, ykwidth, ykheight).text_frame
            p1 = p2 = p3 = ''
            if (len(filename) > 1):
                if (filename.find('市场') > -1):
                    p1 = filename[:filename.find('市场') + 2]
                if (filename.find('小区') > -1):
                    p1 = filename[:filename.find('小区') + 2]
                if (filename.find('商场') > -1):
                    p1 = filename[:filename.find('商场') + 2]
                if (filename.find('期') > -1):
                    p1 = filename[:filename.find('期') + 1]
                if (filename.find('南区') > -1):
                    p1 = filename[:filename.find('南区') + 2]
                if (filename.find('北区') > -1):
                    p1 = filename[:filename.find('北区') + 2]

                if (len(p1) == len(filename)):
                    p1 = filename[:7]
                    if (len(filename) - len(p1) > 7):
                        p2 = filename[len(p1):14]
                        p3 = filename[14:]
                    else:
                        p2 = filename[len(p1):]
                else:
                    if (len(p1) == 0):
                        p1 = filename[:7]
                    if (len(filename) - len(p1) > 7):
                        p2 = filename[len(p1):14]
                        p3 = filename[14:]
                    else:
                        p2 = filename[len(p1):]

            # print('file:'+filename+',p1:'+p1+',p2:'+p2+',p3:'+p3+'.')
            if (len(p1) > 0):
                tx1.paragraphs[0].text = p1
                tx1.paragraphs[0].font.name = '微软雅黑'
                tx1.paragraphs[0].font.size = Pt(30)
                tx1.paragraphs[0].alignment = PP_ALIGN.CENTER  # 文本内容对齐方式 居中
                tx1.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
            if (len(p2) > 0):
                tx1.add_paragraph()
                tx1.paragraphs[1].text = p2
                tx1.paragraphs[1].font.name = '微软雅黑'
                tx1.paragraphs[1].font.size = Pt(30)
                tx1.paragraphs[1].alignment = PP_ALIGN.CENTER  # 文本内容对齐方式 居中
                tx1.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
            if (len(p3) > 0):
                tx1.add_paragraph()
                tx1.paragraphs[2].text = p3
                tx1.paragraphs[2].font.name = '微软雅黑'
                tx1.paragraphs[2].font.size = Pt(30)
                tx1.paragraphs[2].alignment = PP_ALIGN.CENTER  # 文本内容对齐方式 居中
                tx1.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
            # tx1.font.size = Pt(20)
            # slide.shapes.placeholders[0].text = filename
            slide.shapes.add_picture(filePath, lleft, ltop, lwidth, lheight)


# 当前路径
rootdir = os.getcwd()
# 取当前路径文件名
list = os.listdir(rootdir)
# 遍历项目为计算涉及到项目的图片数量
for key, value in dict.items():
    for va in value:
        for ll in range(0, len(list)):
            if (list[ll].find(va) > -1):
                if (key in sjwy):
                    sjwy[key] = sjwy[key] + 1
                else:
                    sjwy[key] = 1

slide = prs.slides[0]
js = 0
# #修改首页时间内容
for j in range(0, len(slide.shapes)):
    shape = slide.shapes[j]
    if (shape.has_text_frame):
        shape1 = shape.text_frame
        if (j == 0):
            shape1.text = nowtime1
            ls1 = get_current_week()
            shape1.text = ls1

## 添加首页内容
lsjs = 4
for wy, val in sjwy.items():

    shapes1 = slide.shapes[lsjs].text_frame
    shapes1.paragraphs[0].font.underline = True
    shapes1.paragraphs[0].font.name = '微软雅黑'
    shapes1.text = wy
    # 添加包括内容
    for val in dict[wy]:
        add_ye(val)

    # 添加链接
    slide.shapes[lsjs].click_action.target_slide = prs.slides[ksye]
    ksye = ksye + sjwy[wy]
    lsjs += 2
    slide_1 = prs.slides[ksye - 1]
    shapes5 = slide_1.shapes
    shape2 = shapes5.add_shape(MSO_SHAPE.ACTION_BUTTON_BEGINNING, left=Inches(12.5), top=Inches(6.9),
                               width=Inches(0.35), height=Inches(0.35))
    shape2.click_action.target_slide = prs.slides[0]
# #删除空余内容

for kk in range(4 + len(sjwy) * 2 - 1, 25):
    # print(str(kk))

    slide.shapes[kk].text_frame.text = ''

save_name = '检查情况' + nowtime + '.pptx'
prs.save(save_name)

# print(sjwy)

print('————————————————————————————————————————————————')
print('文件：' + save_name + '生成！')
print('————————————————————————————————————————————————')